﻿namespace SharePointCSVImporter
{
    using System.IO.IsolatedStorage;

    internal static class ConfigManager
    {
        #region Fields

        const string constDupActionKey = "CSVImportDupAction";
        const string constOverwriteKey = "CSVImportOverwrite";
        const string constStoreCsvColumns = "CSVImportMaps";
        const string constStoreDefaultExtList = "CSVImportDefaultExternalLookupList";
        const string constStoreDefaultExtWeb = "CSVImportDefaultExternalLookupWeb";
        const string constStoreFieldMappings = "CSVImportMaps";
        const string constStoreListKey = "CSVImportList";
        const string constStoreWebKey = "CSVImportWeb";

        #endregion Fields

        #region Methods

        internal static void ClearStorage()
        {
            if (IsolatedStorageFile.IsEnabled)
            {
                IsolatedStorageSettings.SiteSettings.Clear();
            }
        }

        internal static object GetFromStorage(string key)
        {
            object rvalue;
            if (IsolatedStorageFile.IsEnabled)
            {
                var settings = IsolatedStorageSettings.SiteSettings;

                if (settings.Contains(key))
                {
                    try
                    {
                        rvalue = settings[key];

                    }
                    catch
                    {
                        rvalue = null;
                    }
                }
                else
                {
                    rvalue = null;
                }
            }
            else
            {
                rvalue = null;
            }
            return rvalue;
        }

        internal static void Save(string key, object value)
        {
            if (IsolatedStorageFile.IsEnabled)
            {
                var settings = IsolatedStorageSettings.SiteSettings;

                if (settings.Contains(key))
                {
                    settings[key] = value;
                }
                else
                {
                    settings.Add(key, value);
                }
            }
        }

        internal static void SaveColumns(System.Collections.Generic.List<string> csvColumns)
        {
            Save(constStoreCsvColumns, csvColumns);
        }

        internal static void SaveDefaultExternalListName(string listName)
        {
            Save(constStoreDefaultExtList, listName);
        }

        internal static void SaveDefaultExternalWebUrl(string url)
        {
            Save(constStoreDefaultExtWeb, url);
        }

        internal static void SaveDupAction(int action)
        {
            Save(constDupActionKey, action);
        }

        internal static void SaveFieldMappings(System.Collections.Generic.List<CSVParser.FieldMapping> fieldMappings)
        {
            Save(constStoreFieldMappings, fieldMappings);
        }

        internal static void SaveOverwriteOption(bool overwrite)
        {
            Save(constOverwriteKey, overwrite);
        }

        internal static void SaveTargetListName(string targetListName)
        {
            Save(constStoreListKey, targetListName);
        }

        internal static void SaveWebUrl(string targetWebUrl)
        {
            Save(constStoreWebKey, targetWebUrl);
        }

        internal static System.Collections.Generic.List<string> TryGetColumnsFromStorage()
        {
            System.Collections.Generic.List<string> rvalue = null;
            object store = GetFromStorage(constStoreCsvColumns);
            if (store != null)
            {
                try
                {
                    rvalue = (System.Collections.Generic.List<string>)store;
                }
                catch { }
            }
            return rvalue;
        }

        internal static string TryGetDefaultExternalListName()
        {
            string rvalue = null;
            object store = GetFromStorage(constStoreDefaultExtList);
            if (store != null)
            {
                try
                {
                    rvalue = (string)store;
                }
                catch { }
            }
            return rvalue;
        }

        internal static string TryGetDefaultExternalWebUrl()
        {
            string rvalue = null;
            object store = GetFromStorage(constStoreDefaultExtWeb);
            if (store != null)
            {
                try
                {
                    rvalue = (string)store;
                }
                catch { }
            }
            return rvalue;
        }

        internal static int TryGetDupAction()
        {
            int rvalue = 0;
            object store = GetFromStorage(constDupActionKey);
            if (store != null)
            {
                rvalue = (int)store;
            }
            return rvalue;
        }

        internal static System.Collections.Generic.List<CSVParser.FieldMapping> TryGetFieldMapingsFromStorage()
        {
            System.Collections.Generic.List<CSVParser.FieldMapping> rvalue = null;
            object store = GetFromStorage(constStoreFieldMappings);
            if (store != null)
            {
                try
                {
                    rvalue = (System.Collections.Generic.List<CSVParser.FieldMapping>)store;
                }
                catch { }
            }
            return rvalue;
        }

        internal static string TryGetListNameFromStorage()
        {
            string rvalue = null;
            object store = GetFromStorage(constStoreListKey);
            if (store != null)
            {
                try
                {
                    rvalue = (string)store;
                }
                catch { }
            }
            return rvalue;
        }

        internal static bool TryGetOverwriteOption()
        {
            bool rvalue = false;
            object store = GetFromStorage(constOverwriteKey);
            if (store != null)
            {
                rvalue = (bool)store;
            }
            return rvalue;
        }

        internal static string TrygetWebFromStorage()
        {
            string rvalue = null;
            object store = GetFromStorage(constStoreWebKey);
            if (store != null)
            {
                try
                {
                    rvalue = (string)store;
                }
                catch { }
            }
            return rvalue;
        }

        #endregion Methods
    }
}